package com.iotek.util;

import java.io.InputStream;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class QNYUtil {
	private static final String ACCESSKEY = "r7XyRTmvzsYD8oX0EgVF9JIGnP6-W4nB4nLM_2cS";
	private static final String SECRETKEY = "GXG0uPTwTAUSYA-d_ZGo18WO28lWcJLjL_qLbDNr";
	private static final String BUCKET = "xiangmu-image";
	private static final String URLHOST = "http://pd6ko05n0.bkt.clouddn.com/";
	
	
	public static String upToQNY(InputStream stream,String fileName) {

		Configuration cfg = new Configuration(Zone.zone1());
		UploadManager uploadManager = new UploadManager(cfg);
		Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
		String upToken = auth.uploadToken(BUCKET);
		
		try {
		    Response response = uploadManager.put(stream, fileName, upToken,null,null);
		    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
		    return URLHOST+putRet.key;
		} catch (QiniuException ex) {
		    Response r = ex.response;
		    System.out.println(r.toString());
		    try {
		        System.out.println(r.bodyString());
		    } catch (QiniuException ex2) {	        
		    }
		}
		return null;	
	}

}
